<template>
  <el-drawer
    v-model="dialogVisible"
    :title="dialogStatus==='create'?'新增': (dialogStatus === 'view' ? '查看' : '编辑')"
    width="600px"
    @close="reset"
  >
    <el-form 
      ref="articleFormRef"
      :model="tableItem" 
      :rules="rules"
      label-width="100"
      label-position="top"
      :disabled="!isDisabled"
    >

      <el-form-item label="项目" prop="projectId">
        <el-select v-model="tableItem.projectId" placeholder="选择一个项目">
          <el-option 
            v-for="item in projectList"
            :label="item.projectName" 
            :value="item.id" 
          />
        </el-select>
      </el-form-item>

      <el-form-item label="房间号" prop="houseIdList">
        <el-select v-model="tableItem.houseIdList" multiple placeholder="可以选择多个房间">
          <el-option 
            v-for="item of homeNumList"
            :label="item.houseNum" 
            :value="item.id" 
          />
        </el-select>
      </el-form-item>

      <el-form-item label="用户名" prop="userName">
        <el-input v-model="tableItem.userName" />
      </el-form-item>

      <el-form-item label="密码" prop="password" v-if="!(dialogStatus==='view')">
        <el-input 
          v-model="tableItem.password"
          type="password"
          show-password
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="tableItem.phone" />
      </el-form-item>

      <el-form-item label="联系地址" prop="address">
        <el-input v-model="tableItem.address" />
      </el-form-item>

      <el-form-item label="报警金额" prop="alarmAmount">
        <el-input v-model="tableItem.alarmAmount" />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="tableItem.remark" />
      </el-form-item>

    </el-form>
    <template #footer v-if="isDisabled">
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >确 定</el-button>
      </span>
    </template>
  </el-drawer>
</template>
<script lang="ts" setup>
import { ref, reactive, inject,watch } from 'vue'
import { ElMessage } from 'element-plus'
import { addUser, editUser,getHouseIdList, homeNumList } from '@/api/settle'
import { validate } from '@/utils/validate'

const projectList = inject('projectList')
console.log('projectList', projectList)

const isDisabled = ref(true)

const tableItem = reactive({
  projectId: null,
  houseIdList: [],
  userName: '',
  password: '',
  phone: '',
  address: '',
  alarmAmount: '',
  remark: '',
})

const rules = reactive({
  projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
  houseIdList: [{ required: true, message: '请选择房间号', trigger: 'change' }],
})

const dialogVisible = ref(false)
const dialogStatus = ref('create')

const reset = () => {
  if (!articleFormRef) return
  articleFormRef.value.resetFields()
  // isDisabled.value = false
}

const articleFormRef = ref(null)

const emit = defineEmits(['listChange'])

const createData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    addUser({...tableItem})
    .then(() => {
      // TODO use res data
      emit('listChange')
      dialogVisible.value = false
      ElMessage.success('创建成功！')
    })
    .catch(() => {
      dialogVisible.value = false
      ElMessage.error('创建失败！')
    })
  }
}

const updateData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    editUser({...tableItem})
    .then(() => {
      // TODO use res data
      emit('listChange')
      dialogVisible.value = false
      ElMessage.success('更新成功！')
    })
    .catch(() => {
      dialogVisible.value = false
      ElMessage.error('更新失败！')
    })
  }
}

defineExpose({ dialogVisible, tableItem, isDisabled, dialogStatus, reset })
// 房间号相关
const homeNumList = ref<homeNumList>([])
const getHomeNumList = async (v: number) => { 
  const res = await getHouseIdList(v)
  homeNumList.value = res.data
}
watch(() => (tableItem.projectId as unknown as number), getHomeNumList)
</script>
<style lang="scss" scoped>
</style>
